package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.model.PhysicalOrder;
import com.yami.shop.service.PhysicalOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 实物订单控制器
 */
@RestController
@RequestMapping("/physicalOrder")
@Api(tags = "实物订单接口")
public class PhysicalOrderController {

    @Resource
    private PhysicalOrderService physicalOrderService;

    @PostMapping
    @ApiOperation(value = "创建实物订单", notes = "创建一个新的实物订单")
    public ResponseEntity<Void> createPhysicalOrder(@RequestBody PhysicalOrder physicalOrder) {
        return physicalOrderService.createPhysicalOrder(physicalOrder);
    }

    @PutMapping
    @ApiOperation(value = "更新实物订单", notes = "更新实物订单信息")
    public ResponseEntity<Void> updatePhysicalOrder(@RequestBody PhysicalOrder physicalOrder) {
        return physicalOrderService.updatePhysicalOrder(physicalOrder);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取实物订单", notes = "根据ID获取实物订单详情")
    public ResponseEntity<PhysicalOrder> getPhysicalOrderById(
            @ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        return physicalOrderService.getPhysicalOrderById(id);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询实物订单", notes = "分页查询实物订单列表")
    public ResponseEntity<IPage<PhysicalOrder>> listPhysicalOrdersByPage(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "店铺ID") @RequestParam(required = false) Long shopId) {
        Page page = new Page(pageNum, pageSize);
        return physicalOrderService.listPhysicalOrdersByPage(page, shopId);
    }

    @GetMapping("/buyer/{buyerId}")
    @ApiOperation(value = "查询买家订单", notes = "根据买家ID查询订单列表")
    public ResponseEntity<List<PhysicalOrder>> listPhysicalOrdersByBuyerId(
            @ApiParam(value = "买家ID", required = true) @PathVariable Long buyerId) {
        return physicalOrderService.listPhysicalOrdersByBuyerId(buyerId);
    }

    @GetMapping("/tracking/{trackingNumber}")
    @ApiOperation(value = "根据物流单号查询订单", notes = "根据物流单号查询订单列表")
    public ResponseEntity<List<PhysicalOrder>> listPhysicalOrdersByTrackingNumber(
            @ApiParam(value = "物流单号", required = true) @PathVariable String trackingNumber) {
        return physicalOrderService.listPhysicalOrdersByTrackingNumber(trackingNumber);
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation(value = "更新订单状态", notes = "更新指定订单的状态")
    public ResponseEntity<Void> updateOrderStatus(
            @ApiParam(value = "订单ID", required = true) @PathVariable Long id,
            @ApiParam(value = "订单状态", required = true) @PathVariable Integer status) {
        return physicalOrderService.updateOrderStatus(id, status);
    }

    @PutMapping("/{id}/tracking")
    @ApiOperation(value = "更新物流信息", notes = "更新订单的物流单号")
    public ResponseEntity<Void> updateTrackingInfo(
            @ApiParam(value = "订单ID", required = true) @PathVariable Long id,
            @ApiParam(value = "物流单号", required = true) @RequestParam String trackingNumber) {
        return physicalOrderService.updateTrackingInfo(id, trackingNumber);
    }
} 